<?php

namespace common\models\user;

use Yii;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property int $id 用户ID
 * @property string $mobile 加密手机号
 * @property string $idcardno 加密证件号
 * @property string $auth_key 加密密钥
 * @property string $password_hash HASH密码
 * @property string $password_reset_token 重置密码时效Token
 * @property int $company_id 用户隶属单位ID
 * @property int $admin_id 用户管理ID
 * @property int $status 状态
 * @property int $created_at 注册时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 注销时间
 */
class User extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%user}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['idcardno', 'auth_key', 'password_hash'], 'required'],
            [['company_id', 'admin_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['mobile', 'idcardno', 'auth_key'], 'string', 'max' => 32],
            [['password_hash', 'password_reset_token'], 'string', 'max' => 255],
            [['idcardno'], 'unique'],
            [['mobile'], 'unique'],
            [['password_reset_token'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => '用户ID',
            'mobile' => '加密手机号',
            'idcardno' => '加密证件号',
            'auth_key' => '加密密钥',
            'password_hash' => 'HASH密码',
            'password_reset_token' => '重置密码时效Token',
            'company_id' => '用户隶属单位ID',
            'admin_id' => '用户管理ID',
            'status' => '状态',
            'created_at' => '注册时间',
            'updated_at' => '更新时间',
            'deleted_at' => '注销时间',
        ];
    }

    public function getMeta() {
        return $this->hasOne(UserMeta::className(), ['id' => 'id']);
    }

    /**
     * 关联公司信息
     */
    public function getCompany() {
        return $this->hasOne(\common\models\company\Company::className(), ['id' => 'company_id']);
    }

}
